package set;

import java.util.TreeSet;

class Person implements Comparable<Person>{
	String name;
	int age;
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	@Override
	public int compareTo(Person o) {
//		return this.age - o.age;
		return this.name.compareTo(o.name);
	}
}

public class TreeSetDemo {
	public static void main(String[] args) {
		TreeSet<Integer> set = new TreeSet<>();
		set.add(234);
		set.add(111);
		set.add(777);
		set.add(567);
		set.add(123);
		System.out.println(set);
		
		// Integer,Person能做TreeSet的泛型类必须实现Comparable接口
		TreeSet<Person> set2 = new TreeSet<>();
		set2.add(new Person("zs",20));
		set2.add(new Person("lisi",25));
		set2.add(new Person("ws",10));
		set2.add(new Person("ms",18));
		System.out.println(set2);
	}
}
